package com.atguigu.ggkt.vod.controller;


import com.atguigu.ggkt.model.vod.Teacher;
import com.atguigu.ggkt.result.Result;
import com.atguigu.ggkt.vo.vod.TeacherQueryVo;
import com.atguigu.ggkt.vod.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author 杰哥
 * @since 2022-07-29
 */
@Api(tags = "讲师管理接口")
@RestController
@RequestMapping("/admin/vod/teacher")
//@CrossOrigin
public class TeacherController {
    @Resource
    private TeacherService teacherService;

    //http://localhost:8301/admin/vod/teacher/findAll
    //查询所有的讲师
    @ApiOperation("查询所有的讲师")
    @GetMapping("findAll")
    public Result findAllTeacher(){
        //按照时间逆序排序
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("join_date");
        List<Teacher> teachers = teacherService.list(wrapper);
        return Result.ok(teachers).message("查询数据成功");
    }

    //逻辑删除讲师
    @ApiOperation("逻辑删除讲师")
    @DeleteMapping("remove/{id}")
    public Result removeTeacher(@ApiParam(name = "id", value = "ID", required = true) @PathVariable Long id){
        boolean isSuccess = teacherService.removeById(id);
        if (isSuccess)
            return Result.ok(null);
        else
            return Result.fail(null);
    }

    //条件查询分页
    @ApiOperation("条件查询分页")
    @PostMapping("findQueryPage/{current}/{limit}")
    public Result findPage(@PathVariable long current, //当前页
                           @PathVariable long limit,  //每页记录数
                           @RequestBody(required = false) TeacherQueryVo teacherQueryVo)//RequestBody接受传过来的数据是json，required = false非必须
    {
        //创建Page对象
        Page<Teacher> pageParam = new Page<>(current, limit);
        //查询返回分页结果
        IPage<Teacher> pageModel = null;
        //封装条件查询
        QueryWrapper<Teacher> wrapper = null;
        //判断teacherQueryVo查询条件是否为空
        if (null == teacherQueryVo){//查询全部新信息
            wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("join_date");
        }else {
            //获取查询条件
            String name = teacherQueryVo.getName();
            Integer level = teacherQueryVo.getLevel();//职称
            String joinDateBegin = teacherQueryVo.getJoinDateBegin();//入职时间
            String joinDateEnd = teacherQueryVo.getJoinDateEnd();
            //封装条件查询
            wrapper = new QueryWrapper<>();
            if (!StringUtils.isEmpty(name))
                wrapper.like("name", name);
            if (level != null)
                wrapper.eq("level", level);
            if (!StringUtils.isEmpty(joinDateBegin))
                wrapper.ge("join_data", joinDateBegin);
            if (!StringUtils.isEmpty(joinDateEnd))
                wrapper.le("join_date", joinDateEnd);
        }
        pageModel = teacherService.page(pageParam, wrapper);
        return Result.ok(pageModel);
    }

    //添加讲师
    @ApiOperation("添加讲师")
    @PostMapping("saveTeacher")
    public Result saveTeacher(@RequestBody Teacher teacher){
        boolean isSuccess = teacherService.save(teacher);
        if (isSuccess)
            return Result.ok(null);
        else
            return Result.fail(null);
    }

    //修改讲师 - 根据id进行查询
    @ApiOperation("根据id查询")
    @GetMapping("getTeacher/{id}")
    public Result getTeacher(@PathVariable Long id){
        Teacher teacher = teacherService.getById(id);
        return Result.ok(teacher);
    }

    //修改讲师 - 最终实现
    @ApiOperation("修改最终实现")
    @PostMapping("updateTeacher")
    public Result updateTeacher(@RequestBody Teacher teacher){
        boolean isSuccess = teacherService.updateById(teacher);
        if (isSuccess)
            return Result.ok(null);
        else
            return Result.fail(null);

    }

    //批量删除讲师
    @ApiOperation("批量删除讲师")
    @DeleteMapping("removeBatch")
    public Result removeBatch(@RequestBody List<Long> idList){
        boolean isSuccess = teacherService.removeByIds(idList);
        if (isSuccess)
            return Result.ok(null);
        else
            return Result.fail(null);
    }

    @ApiOperation("根据id查询")
    @GetMapping("inner/getTeacher/{id}")
    public Teacher getTeacherLive(@PathVariable Long id) {
        Teacher teacher = teacherService.getById(id);
        return teacher;
    }
}

